<template>
	<view>		
		<u-toast ref="uToast" />
		<u-image src='https://project.gxrunxiang.com/cdn/rx.png' width="100%" height="50vh"></u-image>
		<!-- <u-image src='https://project.gxrunxiang.com/cdn/rx.png' width="100%" height="50vh"></u-image> -->
		
			<!-- #ifndef MP-WEIXIN || MP-QQ || MP-BAIDU -->
		<u-button class="custom-style" open-type="getPhoneNumber|agreePrivacyAuthorization" @getphonenumber="getPhoneNumber" bindagreeprivacyauthorization="handleAgreePrivacyAuthorization" @click="click"
			type="success" loadingText="加载中" size="normal" icon="https://project.gxrunxiang.com/cdn/phone.png" id="login-btn">手机号快捷登录系统</u-button><!--  -->	
			<!-- #endif -->

			
			
			
			<!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
		<!-- <u-button class="custom-style" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" @click="click"
			type="success" loadingText="加载中" size="normal" icon="https://project.gxrunxiang.com/cdn//phone.png" id="login-btn">手机号快捷登录系统</u-button> -->
			<u-button class="custom-style"  @click="towxlogin"
				type="success" loadingText="加载中" size="normal" icon="https://project.gxrunxiang.com/cdn/phone.png" id="login-btn">快捷登录系统</u-button>
				<view  style="text-align: center;">
					<text>
						你好，此服务仅供润象内部人员使用
					</text>
				</view>
		<!-- @click="click" -->
			<!-- #endif -->
			<!-- <u--text align="center" text="切换到账号和密码登录" @click="gotoLoginIndex" size="14" style="margin-top: 15px;" margin="15px 0px 0px 0px"></u--text> -->
			<u-loading-page loading-text="登录中..." :loading="loading"></u-loading-page>
			<!-- <u-popup :show="show"  mode="center" @close="close" :round="10" :closeOnClickOverlay="false">
					<view style="width: 200px; height: 150px; background-color: #fff; border-radius: 20%;">
			           <u-button  open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"  @click="click" custom-style="margin-top: 50px"
			           	type="success" loadingText="加载中" size="normal" icon="https://project.gxrunxiang.com/cdn/phone.png" id="login-btn">手机号快捷登录系统</u-button>
					</view>
			</u-popup> -->
			<u-modal :show="show"  :title="title" :content="content">
				<template slot="confirmButton">
				    <u-button  open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"  @click="click"
				    type="success" loadingText="加载中" size="normal" icon="https://project.gxrunxiang.com/cdn/phone.png" id="login-btn">手机号快捷登录系统</u-button>
				</template>
			</u-modal>
				<!-- <privacyPopup ></privacyPopup> -->
			</view>
</template>

<script>
	import md5 from 'js-md5';
	import {
		getuser,
		getMenu,
		login,
		wxlogin,
		towxlogin
	} from '@/common/api.js';
	import {
		mapMutations
	} from 'vuex'	
	// import privacyPopup from '@/components/easy-components.vue'
	export default {
		data() {
			return {
				code: "",
				loading:false,
				show:false,
				content: "检测到您是第一次登录，请使用手机号快捷登录。",
				oid:null,
			}
		},
		// components:{
		// 	privacyPopup
		// },
		// onLoad() {
		//     wx.getPrivacySetting({
		//       success: res => {
		//         console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
		//         if (res.needAuthorization) {
		//           // 需要弹出隐私协议
		//           this.showPrivacy = true

		//         } else {
		//           // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
		//           // wx.getUserProfile()
		//           // wx.chooseMedia()
		//           // wx.getClipboardData()
		//           // wx.startRecord()
		//         }
		//       },
		//       fail: () => {},
		//       complete: () => {}
		//     })
		//   },
		  
		methods: {
			// ...mapMutations(['setToken', 'setUserInfo']),
			wxlogin()
			{
				wx.login({
				  success :(res)=> {
					  // console.log(res,'wx.res');
				    if (res.code) {
						var user = JSON.parse(uni.getStorageSync("$userinfo"))
				      wxlogin(res.code,user.Id).then(res=>{
						  this.loading=false
						  if (user.Roles == "施工负责人角色")
						  	uni.switchTab({
						  		url: "/pages/index/buildimages"
						  	})
						  else
						  	uni.switchTab({
						  		url: "/pages/index/index"
						  	})
					  })
				    } else {
				      console.log('登录失败！' + res.errMsg)
				    }
				  }
				})
			},
			getPhoneNumber(e) {
				this.loading=true
				console.log(e.detail.code,'codecode');
				this.code = e.detail.code;
				if(e.detail.errno){
					this.loading=false
					this.$refs.uToast.show({
						message: e.detail.errno + e.detail.errMsg,
						type: 'error',
					})
					return
				}
				getuser(this.code,this.oid).then(res => {
						if (res.code == 0) {
							uni.removeStorageSync("$userinfo")
							uni.removeStorageSync("$token")
							const {
								user,
								permissions,
								roles
							} = res.data
							// this.user=user					
							uni.setStorageSync("$token", res.data.token)
							uni.setStorageSync("$userinfo", JSON.stringify(user))
							this.gotoindex(user)
							// this.wxlogin()

						} else {
							this.loading=false
							uni.$u.toast(res.message)
						}
				})
			},
			towxlogin(){
				wx.login({
					success :(res)=> {
						if(res.code){
							towxlogin(res.code).then(res=> {
								console.log(res);
								if(res.code == 0){
									this.$refs.uToast.show({
										message: "登录成功。",
										type: 'success',
										position: 'top'
									})
									uni.removeStorageSync("$userinfo")
									uni.removeStorageSync("$token")
									const {
										user,
										permissions,
										roles
									} = res.data
									uni.setStorageSync("$token", res.data.token)
									uni.setStorageSync("$userinfo", JSON.stringify(user))
									// console.log(user.Roles);
									this.gotoindex(user)
								}
								else{
									this.show = true
									this.oid=res.openid
								}
							})
						}
					}
				})
			},
			gotoindex(user){
				this.loading=false
				if (user.Roles == "施工负责人角色")
					uni.switchTab({
						url: "/pages/index/buildimages"
					})
				else
					uni.switchTab({
						url: "/pages/index/index"
					})
			},
			click() {
				login({
					UserName: '梁肯',
					Password: md5('Rx20210528!')
				}).then(res => {
					uni.removeStorageSync("$userinfo")
					uni.removeStorageSync("$token")
					const {
						user,
						permissions,
						roles
					} = res.data
					this.setToken(res.data.token)
					this.setUserInfo(user)
					// console.log(this.$store.getters.$token)
					// console.log(this.$store.getters.$userInfo)
		
					// uni.switchTab({
					// 	url: "pages/index/index"
					// })
					
					this.$router.push("pages/index/index")
				})

			},
			close(){
				this.show = false
			}
			// gotoLoginIndex(){
			// 	uni.redirectTo({
			// 		url: "/pages/login/index"
			// 	})
			// }
		}
	}
</script>

<style>
	.custom-style {

		position: fixed;
		top:50%
	}
	.login-modal{
		display: flex;
		flex-direction: column;
	}
	.view-button{
		display: flex;
		justify-content: space-between;
	}
</style>
